// File Name: oled_ctrl.h
// Author: kukukukuku~
// Date: 2024-07-04

#ifndef __OLED_CTRL_H
#define __OLED_CTRL_H

#include "at32f421.h"

#define OLED_GPIO_CRM                               CRM_GPIOB_PERIPH_CLOCK
#define OLED_SDA_GPIO_PORT                          GPIOB
#define OLED_SDA_GPIO_PIN                           GPIO_PINS_10
#define OLED_SCL_GPIO_PORT                          GPIOB
#define OLED_SCL_GPIO_PIN                           GPIO_PINS_11

#define OLED_IIC_SDA_HIGH                           gpio_bits_set(OLED_SDA_GPIO_PORT, OLED_SDA_GPIO_PIN)
#define OLED_IIC_SDA_LOW                            gpio_bits_reset(OLED_SDA_GPIO_PORT, OLED_SDA_GPIO_PIN)

#define OLED_IIC_SCL_HIGH                           gpio_bits_set(OLED_SCL_GPIO_PORT, OLED_SCL_GPIO_PIN)
#define OLED_IIC_SCL_LOW                            gpio_bits_reset(OLED_SCL_GPIO_PORT, OLED_SCL_GPIO_PIN)

#define OLED_PAGE_MAX                               (4)
#define OLED_COLUMN_MAX                             (128)

#define OLED_MAIN_PAGE_REFRESH_INTERVAL             (2000)
#define OLED_MAIN_PAGE_REFRESH_INTERVAL_MIN         (1000)
#define OLED_MAIN_PAGE_REFRESH_INTERVAL_MAX         (60000)

//一级菜单列表
#define OLED_LEVEL1_KEY_INDEX                       (0)
#define OLED_LEVEL1_LED_INDEX                       (1)
#define OLED_LEVEL1_OLED_INDEX                      (2)
#define OLED_LEVEL1_RS485_INDEX                     (3)
#define OLED_LEVEL1_STEP_MOTOR_INDEX                (4)
#define OLED_LEVEL1_WS2812B_INDEX                   (5)
#define OLED_LEVEL1_SAVE_INDEX                      (6)
#define OLED_LEVEL1_RESET_INDEX                     (7)

//二级菜单列表
#define OLED_LEVEL2_KEY_CLICK_BEEP_INDEX            (0)
#define OLED_LEVEL2_KEY_LONG_BEEP_INDEX             (1)
#define OLED_LEVEL2_KEY_LOCK_INDEX                  (2)

#define OLED_LEVEL2_LED1_INDEX                      (0)
#define OLED_LEVEL2_LED2_INDEX                      (1)

#define OLED_LEVEL2_OLED_OFF_INDEX                  (0)
#define OLED_LEVEL2_OLED_DIR_INDEX                  (1)
#define OLED_LEVEL2_OLED_LIGHT_INDEX                (2)
#define OLED_LEVEL2_OLED_CURSOR_STYLE_INDEX         (3)
#define OLED_LEVEL2_OLED_REFRESH_MS_INDEX           (4)
#define OLED_LEVEL2_OLED_MENU_NUMBER_INDEX          (5)

#define OLED_LEVEL2_RS485_MODE_INDEX                (0)
#define OLED_LEVEL2_RS485_ID_INDEX                  (1)
#define OLED_LEVEL2_RS485_BAUDRATE_INDEX            (2)
#define OLED_LEVEL2_RS485_ACTIVE_MS_INDEX           (3)

#define OLED_LEVEL2_STEP_MOTOR_MODE_INDEX           (0)
#define OLED_LEVEL2_STEP_MOTOR_SPD_INDEX            (1)

#define OLED_LEVEL2_WS2812B_MODE_INDEX              (0)
#define OLED_LEVEL2_WS2812B_STOP_COLOR_INDEX        (1)
#define OLED_LEVEL2_WS2812B_CW_COLOR_INDEX          (2)
#define OLED_LEVEL2_WS2812B_CCW_COLOR_INDEX         (3)

//三级菜单列表
#define OLED_LEVEL3_LED_MODE_INDEX                  (0)
#define OLED_LEVEL3_LED_BREATH_STEP_INDEX           (1)
#define OLED_LEVEL3_LED_BLINK_FREQ_INDEX            (2)
#define OLED_LEVEL3_LED_ON_INDEX                    (3)
#define OLED_LEVEL3_LED_OFF_INDEX                   (4)

#define OLED_LEVEL3_OLED_DIR_NORMAL_INDEX           (0)
#define OLED_LEVEL3_OLED_DIR_REVERSE_INDEX          (1)

#define OLED_LEVEL3_OLED_LIGHT_HIGH_INDEX           (0)
#define OLED_LEVEL3_OLED_LIGHT_MID_INDEX            (1)
#define OLED_LEVEL3_OLED_LIGHT_LOW_INDEX            (2)

#define OLED_LEVEL3_OLED_CURSOR_SYTLE_1_INDEX       (0)
#define OLED_LEVEL3_OLED_CURSOR_SYTLE_2_INDEX       (1)
#define OLED_LEVEL3_OLED_CURSOR_SYTLE_3_INDEX       (2)
#define OLED_LEVEL3_OLED_CURSOR_SYTLE_4_INDEX       (3)
#define OLED_LEVEL3_OLED_CURSOR_SYTLE_5_INDEX       (4)
#define OLED_LEVEL3_OLED_CURSOR_SYTLE_6_INDEX       (5)
#define OLED_LEVEL3_OLED_CURSOR_SYTLE_7_INDEX       (6)

#define OLED_LEVEL3_OLED_MENU_NUMBER_SHOW_INDEX     (0)
#define OLED_LEVEL3_OLED_MENU_NUMBER_BLANK_INDEX    (1)

#define OLED_LEVEL3_RS485_MODE_ACTIVE_INDEX         (0)
#define OLED_LEVEL3_RS485_MODE_PASSIVE_INDEX        (1)

#define OLED_LEVEL3_RS485_BAUD_9600_INDEX           (0)
#define OLED_LEVEL3_RS485_BAUD_19200_INDEX          (1)
#define OLED_LEVEL3_RS485_BAUD_38400_INDEX          (2)
#define OLED_LEVEL3_RS485_BAUD_57600_INDEX          (3)
#define OLED_LEVEL3_RS485_BAUD_74800_INDEX          (4)
#define OLED_LEVEL3_RS485_BAUD_115200_INDEX         (5)
#define OLED_LEVEL3_RS485_BAUD_230400_INDEX         (6)
#define OLED_LEVEL3_RS485_BAUD_460800_INDEX         (7)
#define OLED_LEVEL3_RS485_BAUD_500000_INDEX         (8)
#define OLED_LEVEL3_RS485_BAUD_576000_INDEX         (9)

#define OLED_LEVEL3_STEP_MOTOR_MODE_FULL_INDEX      (0)
#define OLED_LEVEL3_STEP_MOTOR_MODE_HALF_INDEX      (1)
#define OLED_LEVEL3_STEP_MOTOR_MODE_QUARTER_INDEX   (2)
#define OLED_LEVEL3_STEP_MOTOR_MODE_EIGHTH_INDEX    (3)
#define OLED_LEVEL3_STEP_MOTOR_MODE_SIXTEENTH_INDEX (4)

#define OLED_LEVEL3_STEP_MOTOR_SPD_ULTRA_FAST_INDEX (0)
#define OLED_LEVEL3_STEP_MOTOR_SPD_FAST_INDEX       (1)
#define OLED_LEVEL3_STEP_MOTOR_SPD_NORMAL_INDEX     (2)
#define OLED_LEVEL3_STEP_MOTOR_SPD_SLOW_INDEX       (3)
#define OLED_LEVEL3_STEP_MOTOR_SPD_ULTRA_SLOW_INDEX (4)

#define OLED_LEVEL3_WS2812B_WORK_MODE_STEP_INDEX    (0)
#define OLED_LEVEL3_WS2812B_WORK_MODE_PICKER_INDEX  (1)

#define OLED_LEVEL3_WS2812B_MODE_RANDOM_INDEX       (0)
#define OLED_LEVEL3_WS2812B_MODE_FIXED_INDEX        (1)

//四级菜单列表
#define OLED_LEVEL4_LED_MODE_BREATH_INDEX           (0)
#define OLED_LEVEL4_LED_MODE_BLINK_INDEX            (1)
#define OLED_LEVEL4_LED_MODE_SWITCH_INDEX           (2)

#define OLED_LEVEL4_WS2812B_RED_INDEX               (0)
#define OLED_LEVEL4_WS2812B_GREEN_INDEX             (1)
#define OLED_LEVEL4_WS2812B_BLUE_INDEX              (2)

//OLED发送模式
typedef enum OledSendMode
{
    OLED_SEND_CMD,
    OLED_SEND_DATA,
}OledSendMode;

//OLED菜单选项对象
typedef struct OledMenuItem
{
    uint8_t *itemName;
    const uint8_t nextLevelFlag;  //是否有下一级菜单
}OledMenuItem, *pOledMenuItem;

//OLED菜单对象
typedef struct OledMenu
{
    pOledMenuItem item;
    uint8_t itemNum;
    int8_t itemIndex;           //选项下标
    int8_t itemIndexLast;       //选项上一个下标
    int8_t oledRowIndex;        //选项所在OLED行下标
    int8_t oledRowIndexLast;    //选项所在OLED行上一个下标
}OledMenu, *pOledMenu;

extern OledMenu oledMenuOption; //设置界面对象
extern OledMenu oledMenuKey;    //Key界面对象
extern OledMenu oledMenuLed;    //LED界面对象
extern OledMenu oledMenuLedOption;  //LED设置界面对象
extern OledMenu oledMenuLedMode;    //LED模式界面对象
extern OledMenu oledMenuOled;       //OLED界面对象
extern OledMenu oledMenuOledDir;    //OLED方向界面对象
extern OledMenu oledMenuOledLight;  //OLED亮度界面对象
extern OledMenu oledMenuOledCursorStyle;    //OLED光标风格界面对象
extern OledMenu oledMenuOledMenuNumber;     //OLED菜单编号界面对象
extern OledMenu oledMenuRs485;      //RS485界面对象
extern OledMenu oledMenuRs485Mode;  //RS485模式界面对象
extern OledMenu oledMenuRs485Baud;  //RS485波特率界面对象
extern OledMenu oledMenuStepMotor;  //Step motor界面对象
extern OledMenu oledMenuStepMotorMode;  //Step motor模式界面对象
extern OledMenu oledMenuStepMotorSpd;   //Step motor转速界面对象
extern OledMenu oledMenuWs2812b;        //WS2812B界面对象
extern OledMenu oledMenuWs2812bWorkMode;//WS2812B工作模式界面对象
extern OledMenu oledMenuWs2812bMode;    //WS2812模式界面对象
extern OledMenu oledMenuWs2812bFixed;   //WS2812固定值界面对象

extern uint16_t oledMainPageRefreshInterval;   //OLED主界面刷新时间间隔(ms)

extern uint8_t oledSelectLevel1;   //OLED一级设置菜单所选编号
extern uint8_t oledSelectLevel2;   //OLED二级设置菜单所选编号
extern uint8_t oledSelectLevel3;   //OLED三级设置菜单所选编号
extern uint8_t oledSelectLevel4;   //OLED四级设置菜单所选编号

extern uint8_t oledDisModeFlag;    //OLED显示主界面或设置界面标志
extern uint8_t oledMenuLevel;      //OLED菜单层级

extern uint8_t oledItemUpDnEnaFlag;    //OLED菜单选项上下移使能标志
extern uint8_t oledItemUpFlag;         //OLED菜单选项上移标志
extern uint8_t oledItemDnFlag;         //OLED菜单选项下移标志
extern uint8_t oledItemSelectFlag;     //OLED菜单选项确认标志

extern uint8_t oledValSetEnaFlag;  //OLED设置数值使能标志
extern uint8_t oledValSetSubFlag;  //OLED设置数值减小标志
extern uint8_t oledValSetPlusFlag; //OLED设置数值增大标志

extern uint8_t oledOffFlag;        //OLED关闭显示
extern uint8_t oledDir;            //OLED方向
extern uint8_t oledLightLevel;     //OLED亮度等级
extern uint8_t oledCursorStyle;    //OLED光标风格
extern uint8_t oledMenuNumberFlag; //OLED菜单编号标志
extern uint8_t oledMenuNumberFlagLast; //OLED菜单编号标志

extern uint8_t oledWarnOledAwakenedFlag;   //OLED警告OLED已唤醒标志
extern uint8_t oledWarnKeyLockedFlag;  //OLED警告按键已锁定标志
extern uint8_t oledWarnKeyUnlockedFlag;    //OLED警告按键已解锁标志

//OLED IIC发送数据/命令
void oled_iic_send_data(uint8_t data, OledSendMode oledSendMode);

//OLED配置
void oled_config(void);

#endif //__OLED_CTRL_H
